﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeekService;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace PeekWebApp.handler
{
    /// <summary>
    /// SearchHandler 的摘要说明
    /// </summary>
    public class SearchHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string keywords =context.Server.HtmlDecode(context.Request["keywords"]);
            int psize = int.Parse(context.Request["psize"]);
            int pindex = int.Parse(context.Request["pindex"]);
            string typecode = context.Request["typeCode"];
            int recordCount=0;
            List<TourismInfo> tourList = new ExtendService().Search(keywords,pindex,psize,out recordCount,typecode);
            int pageCount = 0;
            if (recordCount % psize == 0)
            {
                pageCount = recordCount / psize;
            }
            else {
                pageCount = recordCount / psize + 1;
            }
                context.Response.Write(JsonConvert.SerializeObject(new { Data = new { Rows = tourList, Pager = new { PageIndex = pindex, Psize = psize, PageCount = pageCount } } }));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}